<table>
	<caption>
		<label for="recordType"><g:message code="b2b.salesOrder.recordType" default="Record Type" /></label>:
		${salesOrderInstance?.recordType?.encodeAsHTML()}
		<g:hiddenField name="recordType.id" value="${salesOrderInstance?.recordType?.id}" />
	</caption>
	<tbody>
        <tr class="prop">
            <td  class="name"><g:message code="b2b.salesOrder.dealer" default="Dealer" /></td>
            <td  class="value">${salesOrderInstance?.dealer?.encodeAsHTML()}</td>
            <td  class="name"><g:message code="b2b.salesOrder.department" default="Department" /></td>
            <td  class="value">${salesOrderInstance?.dealer?.department?.encodeAsHTML()}</td>
            <td  class="name"><g:message code="b2b.salesOrder.orderDate" default="Order Date" /></td>
            <td  class="value"><g:formatDate format="yyyy-MM-dd" date="${salesOrderInstance?.orderDate}" /></td>
        </tr>
        <tr class="prop">
            <td  class="name"><g:message code="b2b.salesOrder.industry" default="Industry" /></td>
            <td  class="value"><g:link controller="channel" action="show" id="${salesOrderInstance?.industry?.id}">${salesOrderInstance?.industry?.encodeAsHTML()}</g:link></td>
            <td class="name"><g:message code="b2b.salesOrder.project" default="Project" /></td>
            <td  class="value">${fieldValue(bean: salesOrderInstance, field: "project")}</td>
            <td  class="name"><g:message code="b2b.salesOrder.effectiveDate" default="Effective Date" /></td>
            <td  class="value"><g:formatDate format="yyyy-MM-dd" date="${salesOrderInstance?.effectiveDate}" /></td>
        </tr>
        
        <tr class="prop">
            <td  class="name"><g:message code="b2b.salesOrder.id" default="Id" /></td>
            <td  class="value">${fieldValue(bean: salesOrderInstance, field: "id")}</td>
            <td  class="name"><g:message code="b2b.salesOrder.serialNumber" default="ERP Order Number" /></td>
            <td  class="value">${fieldValue(bean: salesOrderInstance, field: "serialNumber")}</td>
            <td  class="name"><g:message code="b2b.salesOrder.accountSerialNumber" default="Account Order Number" /></td>
            <td  class="value">${fieldValue(bean: salesOrderInstance, field: "accountSerialNumber")}</td>
        </tr>
        
        <tr class="prop">
            <td  class="name"><g:message code="b2b.salesOrder.salesMan" default="Sales Man" /></td>
            <td  class="value"><g:link controller="staff" action="show" id="${salesOrderInstance?.salesMan?.id}">${salesOrderInstance?.salesMan?.encodeAsHTML()}</g:link></td>
            <td class="name"><g:message code="b2b.salesOrder.status" default="Status" /></td>
            <td class="value">${fieldValue(bean: salesOrderInstance, field: "status")}</td>
            <td class="name">&nbsp;</td>
            <td class="value">&nbsp;</td>
        </tr>

        <tr class="prop">
            <td  class="name"><g:message code="b2b.salesOrder.address1.label" default="Address(1)" /></td>            
            <td  class="value" colspan="7">${salesOrderInstance?.address1?.encodeAsHTML()}</td>
        </tr>

        <tr class="prop">
            <td  class="name"><g:message code="b2b.salesOrder.address2.label" default="Address(2)" /></td>            
            <td  class="value" colspan="7">${salesOrderInstance?.address2?.encodeAsHTML()}</td>
        </tr>

        <tr class="prop">
            <td  class="name"><g:message code="b2b.salesOrder.description" default="Desc" /></td>
            <td  class="value" colspan="7">${fieldValue(bean: salesOrderInstance, field: "description")}</td>
        </tr>
        
        <tr class="prop">
            <td  class="name"><g:message code="b2b.salesOrder.owner" default="Owner" /></td>
            <td  class="value"><g:link controller="user" action="show" id="${salesOrderInstance?.owner?.id}">${salesOrderInstance?.owner?.encodeAsHTML()}</g:link></td>
            <td class="name"><g:message code="b2b.salesOrder.createdBy" default="Created By" /></td>
            <td class="value">${fieldValue(bean: salesOrderInstance, field: "createdBy")},${fieldValue(bean: salesOrderInstance, field: "dateCreated")}</td>
            <td class="name"><g:message code="b2b.salesOrder.lastModifiedBy" default="Last Modified By" /></td>
            <td class="value">${fieldValue(bean: salesOrderInstance, field: "lastModifiedBy")},${fieldValue(bean: salesOrderInstance, field: "lastUpdated")}</td>
        </tr>
        
    </tbody>
</table>

<table>
    <thead>
        <tr>
            <th><g:message code="b2b.salesOrderDetail.serialNumber" default="Serial Number" /></th>
            <th><g:message code="b2b.salesOrderDetail.product" default="Product" /></th>
            <th><g:message code="b2b.product.name" default="Product Name" /></th>
            <th><g:message code="b2b.product.standard" default="Standard" /></th>
            <th><g:message code="b2b.product.category" default="Category" /></th>
            <th><g:message code="b2b.salesOrderDetail.price" default="Price" /></th>
            <th><g:message code="b2b.salesOrderDetail.discount" default="Discount" /></th>
            <th><g:message code="b2b.salesOrderDetail.quantity" default="Order Quantity" /></th>
            <th><g:message code="b2b.salesOrderDetail.deliveryLimitation" default="Delivery Limitation" /></th>
            <th><g:message code="b2b.salesOrderDetail.amount" default="Amount" /></th>
            <th><g:message code="b2b.salesOrderDetail.contractDetail" default="Contract Detail" /></th>
            <th><g:message code="b2b.salesOrderDetail.specialDiscount" default="Special Discount" /></th>
            <th><g:message code="b2b.salesOrderDetail.specialAmount" default="Special Amount" /></th>
            <th><g:message code="b2b.salesOrderDetail.finalDiscount" default="Final Discount" /></th>
            <th><g:message code="b2b.salesOrderDetail.finalAmount" default="Final Amount" /></th>
        </tr>
    </thead>
    <tbody id="salesOrderDetailList">
    <g:set var="_total_child_special_amount" value="${0}"></g:set>
    <g:set var="_total_child_final_amount" value="${0}"></g:set>

    <g:each in="${salesOrderInstance.salesOrderDetails}" status="i" var="salesOrderDetailInstance">
        <tr class="prop ${(i % 2) == 0 ? 'odd' : 'even'} " >
	       	<!-- serial number -->
        	<td>
            	${fieldValue(bean: salesOrderDetailInstance, field: "serialNumber")}
            </td>
            <!-- product -->
            <td>${fieldValue(bean: salesOrderDetailInstance, field: "product")}</td>
            <!-- product.name -->
            <td>${fieldValue(bean: salesOrderDetailInstance, field: "product.name")}</td>
            <!-- product.standard -->
            <td>${fieldValue(bean: salesOrderDetailInstance, field: "product.standard")}</td>
            <!-- category -->
            <td>
                ${include(controller : 'salesOrderAjax', action : 'productCategoryOfCG', params : [product : salesOrderDetailInstance?.product?.serialNumber] )}
            </td>
            <!-- price -->
            <td class="number"><g:formatNumber number="${salesOrderDetailInstance?.price}" format="###,##0.000000" /></td>
            <!-- discount -->
            <td class="number"><g:formatNumber number="${salesOrderDetailInstance?.discount}" format="###,##0.00%" /></td>
            <!-- quantity -->
            <td class="number"><g:formatNumber number="${salesOrderDetailInstance?.quantity}" format="###,##0.00" /></td>
            <!-- delivery limitation -->
            <td><g:formatDate format="yyyy-MM-dd" date="${salesOrderDetailInstance?.deliveryLimitation}"/></td>
            <!-- amount -->
            <td class="number"><g:formatNumber number="${salesOrderDetailInstance?.amount}" format="###,##0.0000" /></td>
            <!-- contract Detail -->
            <td>
            	<g:link 
                    controller="contract" action="show" 
                    id="${salesOrderDetailInstance?.contractDetail?.contract?.id}">
                        ${fieldValue(bean: salesOrderDetailInstance, field: "contractDetail")}
                </g:link>
            </td>
            <!-- special discount -->
            <td class="number">
	        	<g:formatNumber number="${salesOrderDetailInstance?.specialDiscount}" format="###,##0.00%" />
            </td>
            <!-- special amount -->
            <td class="number">
	            <g:set var="_total_child_special_amount" value="${(_total_child_special_amount ?: 0) + (salesOrderDetailInstance?.specialAmount ?: 0)}"></g:set>
	         	<g:formatNumber number="${salesOrderDetailInstance?.specialAmount}" format="###,##0.0000" />
            </td>
            <!-- final discount -->
            <td class="number">
	           	<g:formatNumber number="${salesOrderDetailInstance?.finalDiscount}" format="###,##0.00%" />
            </td>
            <!-- final amount -->
            <td class="number">
	       		    <g:set 
						var="_total_child_final_amount" 
						value="${(_total_child_final_amount ?: 0) + (salesOrderDetailInstance?.finalAmount ?: 0)}"></g:set>
	            	<g:formatNumber number="${salesOrderDetailInstance?.finalAmount}" format="###,##0.0000" />
            </td>
       </tr>
	    </g:each>
    </tbody>
    <tfoot>
        <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>
            	<label for="totalAmount"><g:message code="b2b.salesOrder.salesOrderDetails.amount.sum" default="Total Amount" /></label>
            </td>
            <td class="number" id="totalAmount">
				${formatNumber(number:salesOrderInstance?.salesOrderDetails*.amount?.sum(), format:'###,##0.0000') }
            </td>
            <td>&nbsp;</td>
            <td>
            	<label for="totalSpecialAmount"><g:message code="b2b.salesOrder.salesOrderDetails.specialAmount.sum" default="Total Special Amount" /></label>
            </td>
            <td class="number" id="totalSpecialAmount">
				${formatNumber(number:(_total_child_special_amount ?: 0), format:'###,##0.0000') }
            </td>
            <td>
            	<label for="totalFinalAmount"><g:message code="b2b.salesOrder.salesOrderDetails.finalAmount.sum" default="Total Final Amount" /></label>
            </td>
            <td class="number" id="totalFinalAmount">
				${formatNumber(number:_total_child_final_amount ?: 0, format:'###,##0.0000') }
            </td>
        </tr>
    </tfoot>
</table>
<g:render template="/b2b/salesOrder/script_toggle" />
